package casts.audio
{
   import com.greensock.TweenMax;
   import com.greensock.easing.*;
   
   import flash.events.Event;
   import flash.events.MouseEvent;
   import flash.media.Sound;
   import flash.media.SoundChannel;
   import flash.media.SoundTransform;
   import flash.net.URLRequest;
   
   import jp.progression.casts.CastMovieClip;
   import jp.progression.events.ProcessEvent;
   
   /**
    * 背景音樂物件，用物件方式設計，方便管理
    * @author cjboy1984@gmail.com
    * @usage
    * new AddChild(container, snd)
    * new RemoveChild(container, snd)
    * snd.playSound();
    * snd.stopSound();
    */
   public class BGM extends CastMovieClip
   {
      private var snd:Sound;
      private var sndChannel:SoundChannel;
      
      // fla
      public var playing:Boolean;
      
      public function BGM( initObject:Object = null )
      {
         super( initObject );
         
         id = 'bgm';
         group = 'root';
         mouseChildren = false;
         
         stop();
      }
      
      // --------------------- LINE ---------------------
      
      // play audio
      public function playSound():void
      {
         trace("BGM_1.playSound()");
         
         // stop
         stopTheSound();
         // play
         sndChannel = snd.play(0, 0, new SoundTransform(1));
         sndChannel.addEventListener(Event.SOUND_COMPLETE, soundReplay);

         // sound tween
         TweenMax.killTweensOf(sndChannel);
         TweenMax.to(sndChannel, 1, { volume:1 } );

         playing = true;
      }
      
      // stop audio
      public function stopSound(immediately:Boolean = false):void
      {
         if (!sndChannel) return;
         
         trace("BGM_1.stopSound(" + immediately + ")");
         if (immediately)
         {
            stopTheSound();
         }
         else
         {
            TweenMax.killTweensOf(sndChannel);
            TweenMax.to(sndChannel, 1, { volume:0, onComplete:stopTheSound } );
         }
      }
      
      // 或是可以直接存取soundChannel
      public function get soundChannel():SoundChannel { return sndChannel; }
      
      // ################### protected ##################
      
      override protected function atCastAdded():void
      {
         // sound
         snd = new Sound();
         snd.load(new URLRequest('audio/bgm.mp3')); // just load, but not play
         playing = false;
      }
      
      override protected function atCastRemoved():void
      {
         stopTheSound();
      }
      
      // #################### private ###################
      
      private function stopTheSound():void
      {
         playing = false;
         
         if (sndChannel)
         {
            sndChannel.removeEventListener(Event.SOUND_COMPLETE, soundReplay);
            sndChannel.stop();
         }
      }
      
      private function soundReplay(e:Event):void
      {
         sndChannel = snd.play();
         sndChannel.addEventListener(Event.SOUND_COMPLETE, soundReplay);
      }
      
      // --------------------- LINE ---------------------
      
   }
   
}
